<template>
  <view>
    <!-- 页面内容将在这里渲染 -->
    <router-view />
    
  </view>
</template>

<script>
// 导入图片URL处理工具
import { getFullImageUrl } from './utils/request';
// 导入Vue实例
import Vue from 'vue';

export default {
  onLaunch() {
    console.log('App Launch')
    // 全局分享配置
    this.globalShare();
  },
  onShow() {
    console.log('App Show')
    // 延迟更新tabbar状态，确保页面栈已完全加载
    setTimeout(() => {
      this.updateTabBarState();
    }, 50); // 进一步减少延迟时间
  },
  onHide() {
    console.log('App Hide')
  },
  methods: {
    globalShare() {
      // 使用Vue.mixin全局注入分享配置
      const app = this;
      Vue.mixin({
        onShareAppMessage(res) {
          if (this.$options.onShareAppMessage && typeof this.$options.onShareAppMessage === 'function' && this.$options.onShareAppMessage !== Vue.mixinShareAppMessage) {
            return this.$options.onShareAppMessage(res);
          }
          return {
            title: '优质生活服务平台',
            path: '/pages/index-new/index',
            imageUrl: getFullImageUrl('/index/logo.jpg')
          };
        },
        onShareTimeline(res) {
          if (this.$options.onShareTimeline && typeof this.$options.onShareTimeline === 'function' && this.$options.onShareTimeline !== Vue.mixinShareTimeline) {
            return this.$options.onShareTimeline(res);
          }

          return {
            title: '优质生活服务平台',
            query: 'from=timeline',
            imageUrl: getFullImageUrl('/index/logo.jpg')
          };
        }
      });

      // 保存mixin中的分享方法引用，用于后续判断
      Vue.mixinShareAppMessage = Vue.options.methods.onShareAppMessage;
      Vue.mixinShareTimeline = Vue.options.methods.onShareTimeline;
    },

    updateTabBarState() {
      try {
        const pages = getCurrentPages();
        if (!pages || pages.length === 0) return;

        const currentPage = pages[pages.length - 1];
        if (!currentPage || !currentPage.route) return;

        // 获取当前页面路径（清理格式）
        let currentRoute = `/${currentPage.route}`;
        currentRoute = currentRoute.replace(/\/$/, '').split('?')[0];

        console.log('App.vue updateTabBarState called with cleaned route:', currentRoute);

        // 精确的tab页面路径列表
        const tabRoutes = [
          '/pages/index-new/index',
          '/pages/Receiveorders/Receiveorders',
          '/pages/discover/index',
          '/pages/chat/index',
          '/pages/pageMy/index'
        ];

        // 检查是否是tab页面（精确匹配）
        const isTabPage = tabRoutes.some(route =>
          currentRoute === route
        );

        console.log('Is tab page:', isTabPage, 'Current route:', currentRoute);

        if (isTabPage && currentPage.getTabBar) {
          const tabBar = currentPage.getTabBar();
          if (tabBar && tabBar.setSelectedTab) {
            console.log('Calling tabBar.setSelectedTab from App.vue with route:', currentRoute);
            tabBar.setSelectedTab(currentRoute);
            // 移除重复调用，避免状态冲突
          }
        }
      } catch (error) {
        console.error('App.vue updateTabBarState error:', error);
      }
    }
  }
}
</script>

<style>

/* 全局样式 */
page {
  background-color: #F2F3F8;
  height: 100%;
}

/* 适配自定义tabbar */
.tab-bar-page {
  padding-bottom: 120rpx;
  /* 增加底部padding，确保内容不被遮挡 */
}
/* 安全区域适配 */
.safe-area-bottom {
  padding-bottom: calc(env(safe-area-inset-bottom) + 120rpx);
}

/* 通用的页面容器样式，防止内容被tab栏遮挡 */
.page-container,
.container {
  padding-bottom: 120rpx !important;
  /* 确保有足够的底部空间 */
  box-sizing: border-box;
}

/* 底部操作区域增加额外空间 */
.bottom-actions {
  margin-bottom: 120rpx;
}

/* 滚动容器底部间距 */
.scroll-view-bottom {
  padding-bottom: 120rpx;
}


/* 全局隐藏小程序滚动条 */
::-webkit-scrollbar {
  width: 0 !important;
  height: 0 !important;
  color: transparent !important;
  display: none !important;
}

page {
  scrollbar-width: none;
  -ms-overflow-style: none;
}

page::-webkit-scrollbar {
  display: none;
}


</style>
